package com.platform.modules.prj.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.common.utils.CadUtils;
import com.platform.common.utils.Query;
import com.platform.common.utils.StringUtils;
import com.platform.config.DocConstants;
import com.platform.modules.prj.dao.PrjWorkAreaDao;
import com.platform.modules.prj.entity.PrjWorkAreaEntity;
import com.platform.modules.prj.service.PrjWorkAreaService;
import com.platform.modules.sys.entity.SysUserEntity;
import com.platform.util.DocUtil;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

@Service("prjWorkAreaService")
public class PrjWorkAreaServiceImpl extends ServiceImpl<PrjWorkAreaDao, PrjWorkAreaEntity> implements PrjWorkAreaService {

	@Autowired
	DocUtil docUtil;
	
	
	@Override
	public List<PrjWorkAreaEntity> queryList(Map<String, Object> params) {
        params.put("sidx", "T.PARENT_ID,T.CREATE_TIME");
        params.put("asc", false);
		return baseMapper.selectList(params);
	}
	
	
	@Override
    public IPage<PrjWorkAreaEntity> queryPage(Map<String, Object> params) {
        IPage<PrjWorkAreaEntity> page = new Query<PrjWorkAreaEntity>(params).getPage();
        List<PrjWorkAreaEntity> list = baseMapper.selectList(page, params);
        return page.setRecords(list);
    }
	
	@Override
	public boolean save(PrjWorkAreaEntity prjWorkAreaEntity, SysUserEntity sysUserEntity) {
		if(StringUtils.isNullOrEmpty(prjWorkAreaEntity.getAreaId())) {
			prjWorkAreaEntity.setCreateTime(new Date());
			prjWorkAreaEntity.setCreateUserId(sysUserEntity.getUserId());
	    	prjWorkAreaEntity.setPrjId(sysUserEntity.getOrgNo());
			this.baseMapper.insert(prjWorkAreaEntity);
			this.createWorkAreaDrawingDoc(prjWorkAreaEntity);
		}else {
			prjWorkAreaEntity.setUpdateTime(new Date());
			prjWorkAreaEntity.setUpdateUserId(sysUserEntity.getUserId());
			this.baseMapper.updateById(prjWorkAreaEntity);	
		}
		return true;
	}
	
	
	
	/**
	 * @descr 创建工区图纸对应的文档和目录
	 * @param prjWorkAreaEntity
	 */
	private void createWorkAreaDrawingDoc(PrjWorkAreaEntity prjWorkAreaEntity) {
		String docId = docUtil.createDoc("WORK_AREA_DRAWING", prjWorkAreaEntity.getAreaName(), prjWorkAreaEntity.getAreaId());
		String parentId = docUtil.getDoc(docId).getDirId();
		docUtil.createDir( null,  null, "总图", 	parentId, docId, 0);
		docUtil.createDir( null,  null, "建筑", 	parentId, docId, 1);
		docUtil.createDir( null,  null, "结构", 	parentId, docId, 2);
		docUtil.createDir( null,  null, "电气", 	parentId, docId, 3);
		docUtil.createDir( null,  null, "给排水",	parentId, docId, 4);
		docUtil.createDir( null,  null, "暖通", 	parentId, docId, 5);
	}
	
	
	

	@Override
	public void saveDrawing(MultipartFile file, String dirId, String docId) throws IOException {
		//保存图纸
    	String attId = docUtil.createAtt(file, dirId);
		//this.saveDwgThumnail(file.getInputStream(), attId);
	}
	
	
	/**
	 * 生成缩略图
	 * @param is
	 * @param id
	 */
	@Async
	@Override
	public void saveDwgThumnail(InputStream is,String id) {
		System.out.println("创建缩略图开始:"+Thread.currentThread().getName());
		docUtil.addFile(CadUtils.dwg2png(is), DocConstants.THUMBNAIL, id);
		System.out.println("创建缩略图结束");
	}

	
	
	
}
